﻿// *********************************************************************
// [DCOM Productions]
// [Copyright (C) DCOM Productions All rights reserved.]
// *********************************************************************

namespace BitFlex.Windows.Forms {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// Windows Explorer Button
    /// </summary>
    [ToolboxBitmap(typeof(Button))]
    public partial class ExplorerButton : System.Windows.Forms.Button {
        /// <summary>
        /// Instantiates a new instance of the BitFlex.Windows.Forms.ExplorerButton class.
        /// </summary>
        public ExplorerButton() {
            InitializeComponent();
            FlatStyle = System.Windows.Forms.FlatStyle.System;
        }

        #region Platform Invoke

        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        #endregion

        #region Constants

        private const int BM_SETIMAGE = 0x00F7;
        private const int BCM_SETSHIELD = 0x0000160C;

        #endregion

        #region Fields

        private bool m_UseIcon;

        #endregion

        #region Properties

        private Icon m_Icon = null;
        /// <summary>
        /// Gets or Sets the button's icon
        /// </summary>
        [
            Category("Appearance"),
            Description("Gets or Sets the button's icon")
        ]
        public Icon Icon {
            get {
                return m_Icon;
            }
            set {
                m_Icon = value;

                if (m_Icon != null) {
                    m_UseIcon = true;
                    Icon = null;
                }

                SetShield(false);
                SetImage();
            }
        }

        private Bitmap m_Image = null;
        /// <summary>
        /// Gets or Sets the button's image
        /// </summary>
        [
            Category("Appearance"),
            Description("Gets or Sets the button's image")
        ]
        new public Bitmap Image {
            get {
                return m_Image;
            }
            set {
                m_Image = value;

                if (m_Image != null) {
                    m_UseIcon = false;
                    Icon = null;
                }

                SetShield(false);
                SetImage();
            }
        }

        private bool m_UseShield = false;
        /// <summary>
        /// Gets or Sets whether the shield icon will be displayed
        /// </summary>
        [
            Category("Appearance"),
            Description("Gets or Sets whether the shield icon will be displayed")
        ]
        public bool UseShield {
            get {
                return m_UseShield;
            }
            set {
                m_UseShield = value;
                SetShield(m_UseShield);

                if (!m_UseShield) {
                    SetImage();
                }
            }
        }

        #endregion

        #region Methods

        private void SetImage() {
            IntPtr icohWnd = IntPtr.Zero;

            if (!m_UseIcon) {
                if (m_Image != null) {
                    icohWnd = m_Image.GetHicon();
                }
            }
            else {
                if (m_Icon != null) {
                    icohWnd = m_Icon.Handle;
                }
            }

            SendMessage(Handle, BM_SETIMAGE, new IntPtr(1), icohWnd);
        }

        private void SetShield(bool value) {
            SendMessage(Handle, BCM_SETSHIELD, IntPtr.Zero, new IntPtr(m_UseShield ? 1 : 0));
        }

        #endregion
    }
}
